package com.ruoyi.biz.letter.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.biz.letter.domain.BizRecordLetter;
import com.ruoyi.biz.letter.domain.CategoryBase;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Fei.Cong
 * @date 2024-07-13 14:47
 */
public class CategoryUtil {

    public static List<BizCategory> getCacheCategorys() {
        List<BizCategory> categories = SpringUtils.getBean(RedisCache.class).getCacheList("categoryList");
        if (CollUtil.isEmpty(categories)) {
            categories = SpringUtils.getBean(IBizCategoryService.class).list();
            SpringUtils.getBean(RedisCache.class).deleteObject("categoryList");
            SpringUtils.getBean(RedisCache.class).setCacheList("categoryList", categories);
        }
        return categories;
    }

    public static String getCacheCategory(Long categoryId) {
        if (Objects.nonNull(categoryId)) {
            BizCategory category = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheUtil.getCategoryKey(String.valueOf(categoryId)));
            if (Objects.isNull(category)) {
                category = SpringUtils.getBean(IBizCategoryService.class).getById(categoryId);
                SpringUtils.getBean(RedisCache.class).deleteObject(CacheUtil.getCategoryKey(String.valueOf(categoryId)));
                SpringUtils.getBean(RedisCache.class).setCacheObject(CacheUtil.getCategoryKey(String.valueOf(categoryId)), category);
            }
            return Optional.ofNullable(category).map(BizCategory::getCategoryName).orElse("");
        }
        return StrUtil.EMPTY;
    }

    public static void buildCategoryId(CategoryBase category) {
        switch (category.getCategoryIds().length) {
            case 1:
                category.setPCategoryId(Long.parseLong(category.getCategoryIds()[0]));
                category.setSCategoryId(0L);
                break;
            case 2:
                category.setPCategoryId(Long.parseLong(category.getCategoryIds()[0]));
                category.setSCategoryId(Long.parseLong(category.getCategoryIds()[1]));
                break;
            default:
                break;
        }
    }

    public static void getCategoryIds(CategoryBase category) {
        if (ObjectUtil.isAllEmpty(category.getPCategoryId(), category.getSCategoryId())) {
            return;
        }
        if (Objects.equals(category.getSCategoryId(), 0L) || Objects.isNull(category.getSCategoryId())) {
            category.setCategoryIds(new String[]{String.valueOf(category.getPCategoryId())});
        } else {
            category.setCategoryIds(new String[]{String.valueOf(category.getPCategoryId()), String.valueOf(category.getSCategoryId())});
        }

    }

    public static void handleQueryCategoryId(CategoryBase category) {
        if (Objects.nonNull(category.getCategoryIds())) {
            List<Long> pCategoryIds = Lists.newArrayList();
            List<Long> sCategoryIds = Lists.newArrayList();
            for (String categoryId : category.getCategoryIds()) {
                String[] splitId = categoryId.split(",");
                switch (splitId.length) {
                    case 1:
                        pCategoryIds.add(Long.parseLong(splitId[0]));
                        sCategoryIds.add(0L);
                        break;
                    case 2:
                        pCategoryIds.add(Long.parseLong(splitId[0]));
                        sCategoryIds.add(Long.parseLong(splitId[1]));
                        break;
                    default:
                        break;
                }
            }
            category.setQueryFirstCategoryIds(pCategoryIds);
            category.setQuerySecondCategoryIds(sCategoryIds);
        }
    }

}
